package com.ouran.common;

import java.io.Serializable;
import java.util.Set;

import org.apache.commons.lang3.SerializationUtils;
import org.apache.log4j.Logger;
import org.springframework.cache.Cache;
import org.springframework.cache.support.SimpleValueWrapper;
import org.springframework.dao.DataAccessException;
import org.springframework.data.redis.connection.RedisConnection;
import org.springframework.data.redis.core.RedisCallback;
import org.springframework.data.redis.core.RedisTemplate;

/**
 * Cache接口实现
 * 
 * @author lijiajun
 *
 */
public class RedisCache implements Cache {

	Logger logger = Logger.getLogger(RedisCache.class);
	private RedisTemplate<String, Object> redisTemplate;
	private String name;

	@Override
	public void clear() {
		logger.info("-------缓存清理------");
		redisTemplate.execute(new RedisCallback<String>() {
			@Override
			public String doInRedis(RedisConnection connection)
					throws DataAccessException {
				connection.flushDb();
				return "ok";
			}
		});
	}

	@Override
	public void evict(Object key) {
		
		//判断是否userCache缓存，是则批量删除
		if("userCache".equals(key)){
			
			logger.info("-------批量刪除userCache缓存start------");
			//利用正则匹配所有待删除的keys
			Set<String> keys = redisTemplate.keys("UserServiceImpl." + "*");
			for (String str : keys) {
				redisTemplate.execute(new RedisCallback<Long>() {
					@Override
					public Long doInRedis(RedisConnection connection)
							throws DataAccessException {
						logger.info("-------缓存刪除------" + str);
						return connection.del(str.getBytes());
					}
				});
			}
			logger.info("-------批量刪除userCache缓存end------");
			
		}else{
			
			logger.info("-------缓存刪除------" + key.toString());
			final String keyf = key.toString();
			redisTemplate.execute(new RedisCallback<Long>() {
				@Override
				public Long doInRedis(RedisConnection connection)
						throws DataAccessException {
					return connection.del(keyf.getBytes());
				}

			});
			
		}
	}

	@Override
	public ValueWrapper get(Object key) {
		logger.info("------缓存获取-------" + key.toString());
		final String keyf = key.toString();
		Object object = null;
		object = redisTemplate.execute(new RedisCallback<Object>() {
			@Override
			public Object doInRedis(RedisConnection connection)
					throws DataAccessException {
				byte[] key = keyf.getBytes();
				byte[] value = connection.get(key);
				if (value == null) {
					logger.info("------缓存不存在-------");
					return null;
				}
				return SerializationUtils.deserialize(value);
			}
		});
		ValueWrapper obj = (object != null ? new SimpleValueWrapper(object)
				: null);
		logger.info("------获取到内容-------" + obj);
		return obj;
	}

	@Override
	public void put(Object key, Object value) {
		logger.info("-------加入缓存------");
		logger.info("key----:" + key);
		logger.info("value----:" + value);
		final String keyString = key.toString();
		final Object valuef = value;
		final long liveTime = 86400;
		redisTemplate.execute(new RedisCallback<Long>() {
			@Override
			public Long doInRedis(RedisConnection connection)
					throws DataAccessException {
				byte[] keyb = keyString.getBytes();
				byte[] valueb = SerializationUtils
						.serialize((Serializable) valuef);
				connection.set(keyb, valueb);
				if (liveTime > 0) {
					connection.expire(keyb, liveTime);
				}
				return 1L;
			}
		});

	}

	@Override
	public <T> T get(Object arg0, Class<T> arg1) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public String getName() {
		return this.name;
	}

	public void setName(String name) {
		this.name = name;
	}

	@Override
	public Object getNativeCache() {
		return this.redisTemplate;
	}

	@Override
	public ValueWrapper putIfAbsent(Object arg0, Object arg1) {
		// TODO Auto-generated method stub
		return null;
	}

	public RedisTemplate<String, Object> getRedisTemplate() {
		return redisTemplate;
	}

	public void setRedisTemplate(RedisTemplate<String, Object> redisTemplate) {
		this.redisTemplate = redisTemplate;
	}

}